--[[
FilePath: \Lua\View\UIHeroManor\UIHeroManor.lua
庄园界面
--]]

require 'View/UIBase/UIBase'
require "View/CharactorUI/UIBreakStone"

UIHeroManor = UIBase.New(PanelNames.UIHeroManor)
local _M = UIHeroManor

_M.hasInit = false
_M.breakStoneList = nil
_M.selectMasterId = 0
_M.isClick = false
_M.hasInitAttr = false
_M.manorModelId = 0
_M.isAuto = false

function _M.Init(cityId, index)
    _M.cityId = cityId;
    _M.selectIndex = index;
    UIBase.Init(_M)
end

function _M:OnCreate()
    if not _M.hasInit then
        self:RegisterOnClick(self:GetChild("BackButton"), self.OnBack)
        self.hero = self:GetChild("hero"):GetChild("head"):GetChild("HeadIcon");
        self.title = self:GetChild("heroManorName");
        self.list = self:GetChild("list");
        self.btnInfo = self:GetChild("btnQucik");
        self.redCtrl = self.btnInfo:GetChild("UIRedDots"):GetController("c1");
        self.btnReplaceHero = self:GetChild("btnReplaceHero");
        self:SetDynamicList(self.list, self.ItemRender, nil, true);
        self.listAttr = self:GetChild("listAttr");
        self.titleTips = self:GetChild("title");
        self.noCtrl = self:GetController("nothing");
        self.attrTips = self:GetChild("tips");

        self.quicklyMoveInCtrl = self:GetController("quicklyMoveIn");
        local confirmTips = self:GetChild("confirmTips");
        confirmTips:GetChild("tips").text = getLanguage("HeroManorText14");
        confirmTips:GetChild("TitleLb").text = getLanguage("HeroManorText04");
        confirmTips:GetChild("Pop").text = getLanguage("HeroManorText13");
        self.btnCloseTipPanel = confirmTips:GetChild("bgclose");
        self.btnConfirm = confirmTips:GetChild("btnConfirm");
        self.quickStateCtrl = confirmTips:GetController("state");
        self.btnChoose = confirmTips:GetChild("btnChoose");

        -- 加成
        self.btnAttrPreview = self:GetChild("btnAttrPreview");
        self.tipsCtrl = self:GetController("tips");
        local attrNode = self:GetChild("n19");
        self.btnCloseAttrPanel = attrNode:GetChild("bgclose");
        self.attrList = attrNode:GetChild("list"):GetChild("list");

        _M.hasInit = true

        self:addEvent()
    end
end

function _M:addEvent()
    self.btnInfo.onClick:Add(function()
        self.quicklyMoveInCtrl.selectedIndex = 1;
        if logicMgr.HeroManorManager.isHighQuality() then
            self.quickStateCtrl.selectedIndex = 0;
        else
            self.quickStateCtrl.selectedIndex = 1;
        end
    end);
    self.btnReplaceHero.onClick:Add(self.OnClickReplaceHero, self);
    self.btnCloseTipPanel.onClick:Add(function()
        self.quicklyMoveInCtrl.selectedIndex = 0;
    end);
    self.btnConfirm.onClick:Add(self.OnClickInfo, self);
    self.btnAttrPreview.onClick:Add(function()
        self.tipsCtrl.selectedIndex = 1;
        self:showAttrList();
    end);
    self.btnCloseAttrPanel.onClick:Add(function()
        self.tipsCtrl.selectedIndex = 0;
    end);
    self.btnChoose.onClick:Add(function()
        if self.quickStateCtrl.selectedIndex == 1 then
            self.quickStateCtrl.selectedIndex = 0;
        else
            self.quickStateCtrl.selectedIndex = 1;
        end
    end);
    self:RegisterEvent(Config.EventType.FreshManorInfo,self.FreshManorInfo);
end

function _M:removeEvent()
    self.btnInfo.onClick:Clear();
    self.btnReplaceHero.onClick:Clear();
    self.btnCloseTipPanel.onClick:Clear();
    self.btnConfirm.onClick:Clear();
    self.btnAttrPreview.onClick:Clear();
    self.btnCloseAttrPanel.onClick:Clear();
    self.btnChoose.onClick:Clear();
end

-- 显示
function _M:OnEnable()
    self.heroNum = 0;
    self.heroManorModel = dataTable.getDataTable("t_heromanormodel");
    for k, v in pairs(self.heroManorModel) do
        self.heroNum = self.heroNum + 1;
    end
    self.breakStoneList = {};
    self.btnInfo.text = getLanguage("HeroManorText04");
    self.btnReplaceHero.text = getLanguage("HeroManorText05");
    self.titleTips.text = getLanguage("HeroManorText18");
    self:RefreshView();
end

function _M:OnClickInfo()
    if self.isClick then
        return;
    end
    self.quicklyMoveInCtrl.selectedIndex = 0;
    self.isClick = true;
    local state = 1;
    if self.quickStateCtrl.selectedIndex == 1 then
        state = 0;
    end
    self.isAuto = true;
    logicMgr.HeroManorManager.setHighQuality(state == 1);
    HeroManorHandler.sendManorAutoInviteMessage(self.cityId, self.selectIndex, state);
end

function _M:OnClickReplaceHero()
    showUI(PanelNames.UIHeroManorMoveIn, self.cityId, self.selectIndex, 0);
    --self:OnBack();
end

function _M:FreshManorInfo()
    if self.isAuto then
        logicMgr.HeroManager.updateHeroBaseAttr(self.selectMasterId,true);
        TAManager.TrackByDictionary("manor_auto");
        FireEvent(Config.EventType.ReFreshHeroPower);
    end
    self.isAuto = false;
    self.isClick = false;
    self:RefreshView();
end

function _M:RefreshView()
    local manorInfo = logicMgr.HeroManorManager.getManorInfo(self.cityId, self.selectIndex);
    self.selectMasterId = tonumber(manorInfo.masterId);
    local heroInfo = logicMgr.HeroManager.getHeroDatabyPartnerId(manorInfo.masterId, true);
    self.manorModelId = heroInfo["partnerModelId"] or heroInfo["f_HeroID"] or 0;
    logicMgr.HeroManorManager.setHeadIcon(self.hero, self.manorModelId, heroInfo.icon);
    self.title.text = string.format(getLanguage("HeroManorText06"), heroInfo.name);
    self.heroStar = heroInfo.star;
    self.visitors = logicMgr.HeroManorManager.getManorVisitors(manorInfo.masterId) or {};
    self.noCtrl.selectedIndex = 0;
    if self.visitors == nil or #self.visitors < 1 then
        self.noCtrl.selectedIndex = 1;
        self.attrTips.text = getLanguage("HeroManorText21");
    end
    self.list.numItems = self.heroNum;
    if logicMgr.HeroManorManager.isAutoRed(self.cityId, self.selectIndex) then
        self.redCtrl.selectedIndex = 2;
    else
        self.redCtrl.selectedIndex = 0;
    end
    self:RefreshAttr();
end

function _M:RefreshAttr()
    self.listAttr:RemoveChildrenToPool();
    local attrItems = logicMgr.HeroManorManager.getAllAttrByGuest(self.visitors,  self.selectMasterId);
    if attrItems == nil or table.nums(attrItems) < 1 then
        if self.noCtrl.selectedIndex == 0 then
            self.noCtrl.selectedIndex = 1;
            local starCfg = logicMgr.HeroManorManager.getHeroManorData(self.manorModelId, 20, true);
            if starCfg == nil then
                self.attrTips.text = "";
            else
                self.attrTips.text = string.format(getLanguage("HeroManorText20"), EnumConst.StarType[starCfg[1].f_HeroStar / 10]);
            end
        end

    else
        for k, v in pairs(attrItems) do
            local item = self.listAttr:AddItemFromPool();
            logicMgr.HeroManorManager.setAttrItemInfo(item, k, v);
        end
    end
end

function _M:ItemRender(index, go)
    go.onClick:Clear();
    local state = go:GetController("state");
    local hero = go:GetChild("hero");
    local unlockTips = go:GetChild("unlockTips");
    local cfg = self.heroManorModel[tostring(index)];
    if self.heroStar < cfg.f_MasterQuality then
        state.selectedIndex = 0;
        unlockTips.text = string.format(getLanguage("HeroManorText11"), EnumConst.StarType[cfg.f_MasterQuality / 10]);
    else
        if self.visitors[index] == nil then
            state.selectedIndex = 1;
        else
            state.selectedIndex = 2;
            local heroInfo = logicMgr.HeroManager.getHeroDatabyPartnerId(self.visitors[index], true);
            local item = CtrlManager.GetCtrl(PanelNames.HeadRectBItem);
            item.initData(hero, heroInfo);
            item.ShowOwnCount(hero, false);
        end
        go.onClick:Add(function()
            showUI(PanelNames.UIHeroManorMoveIn, self.cityId, self.selectIndex, 1);
            --self:OnBack();
        end);
    end
end

function _M:showAttrList()
    if self.hasInitAttr then
        return;
    end
    self.hasInitAttr = true;
    local data = logicMgr.HeroManorManager.getHeroManorData(self.manorModelId, 20, true);
    self.attrList:RemoveChildrenToPool();
    if data ~= nil then
        for i = 1, #data do
            local item = self.attrList:AddItemFromPool();
            local attrs =  item:GetChild("listAttr");
            local breakstone = item:GetChild("n1");
            local qualityFrame = breakstone:GetChild("frame")
            local qualityIcon = breakstone:GetChild("icon")
            local qualityNumIcon = breakstone:GetChild("numIcon")
            local staegeController = breakstone:GetController("jinjie")
            local maxController = breakstone:GetController("LR");

            if self.breakStoneList[i] == nil then
                self.breakStoneList[i] = UIBreakStone:new(breakstone);
            end
            local star =logicMgr.HeroManager.starToQuality(tonumber(data[i].f_HeroStar));
            local config = G.dataTable.t_iconframe[tostring(star)];
            local isFrame = config.f_Ornaments ~= "";
            qualityFrame.visible = isFrame;
            local atlas = "ui://CharactorUI/";
            if isFrame then
                qualityFrame.url = atlas .. config.f_QualityOrnaments;
            end
            if config.f_QualityText ~= "" then
                qualityIcon.url = atlas .. config.f_QualityText;
                staegeController.selectedIndex = 1;
            else
                staegeController.selectedIndex = 0;
            end
            local quality = tonumber(config.f_Quality);
            self.breakStoneList[i]:setQuality(quality);
            if config.f_Id > 10 then
                staegeController.selectedIndex = 0;
                maxController.selectedIndex = star - 10;
            else
                maxController.selectedIndex = 0;
            end
            qualityNumIcon.url = atlas .. config.f_QualityIcon;

            attrs:RemoveChildrenToPool();
            local attr = decodeJsonStrToTabel(data[i].f_AttrAdd);
            for k, v in pairs(attr) do
                local attrItem = attrs:AddItemFromPool();
                logicMgr.HeroManorManager.setAttrItemInfo(attrItem, v[1], v[2]);
            end
        end
    end
end

-- 隐藏
function _M:OnDisable()

end

-- 卸载
function _M:OnDestroy()
    self:removeEvent();
    if self.breakStoneList ~= nil then
        for k, v in pairs(self.breakStoneList) do
            v:destroy();
        end
        self.breakStoneList = nil;
    end
    self.hasInit = false
    self.selectMasterId = 0;
    self.isClick = false;
    self.hasInitAttr = false;
    self.manorModelId = 0;
    self.isAuto = false;
end

return _M;